package com.ykdl.member.kid.gears;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.DoctorTalkChildListAdapter;
import com.ykdl.member.kid.beans.DoctorTalkChildListBean;
import com.ykdl.member.kid.circle.TopicAndResultActivity;
import com.ykdl.member.kid.doctor.CatalogsDoctorListActivity;
import com.ykdl.member.kid.doctor.ProblemDetailsAcivity;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.models.ArticleBean;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.util.MobileUtils;
import com.ykdl.member.kid.util.RequestAsAccessTokenUtil;
import com.ykdl.member.views.PullToRefreshView;
import com.ykdl.member.views.PullToRefreshView.OnFooterRefreshListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class DoctorTalkChildListActivity extends AbsFragmentActivity{
	
	private DoctorTalkChildListBean doctorTalkChildListBean;
	private DoctorTalkChildListAdapter adapter;
	private int gridview_item_image_height;
	private HttpBaseRequest request;
	
	@Override
	protected void getRequestData() {
		showProgress(this);
		final String url = KidConfig.BASE_TEST_SERVER + "pages/index/doctorsays";
		HttpParameters   parameters = new HttpParameters();
		parameters.put("cursor", cursor + "");
		parameters.put("count", count + "");
		request = Wxxr.getInstance().getRequest(url, parameters);
		TaskManager.startHttpRequest(request, new DoctorTalkChildListTag(), DoctorTalkChildListBean.class);
	}
	class DoctorTalkChildListTag implements ITag{
		@Override
		public void refreshUI(Object t, DataState ds) {
			if(t != null && t instanceof DoctorTalkChildListBean){
				doctorTalkChildListBean = (DoctorTalkChildListBean)t;
				if(doctorTalkChildListBean.isExpired()){
					RequestAsAccessTokenUtil.getInstance(mContext).refreshTokenRequest(request, this, DoctorTalkChildListBean.class);
				}else{
					showdata();
					dismissProgress();
				}
			}
		}
		@Override
		public boolean refreshError(DataParseError parseError) {
			//下拉加载更多时，请求数据错误时，取消正在加载状态
			removeFootRefreshView();
			dismissProgress();
			return false;
		}
		@Override
		public void refreshCancel() {
		}
		
	}
	
	@Override
	protected void setTitle() {
		tv_title.setText("");
		tv_title.setText("医生说育儿");
	}

	@Override
	protected void matchGridView() {
		int screenWidth = MobileUtils.getScreenWidthIntPx();
		int gridview_width = (screenWidth - MobileUtils.dpToPx(12));
		gridview_item_width = gridview_width / 2; 
		gridview_item_image_height = (int)((gridview_item_width  - MobileUtils.dpToPx(12)) / 1.5);
		
		LayoutParams  params = (LayoutParams) mGridView.getLayoutParams();
		params.width = gridview_width;
		params.rightMargin = MobileUtils.dpToPx(6);
		params.leftMargin = MobileUtils.dpToPx(6);
		mGridView.setLayoutParams(params);
		
		
		mGridView.setHorizontalSpacing(MobileUtils.dpToPx(6));
		mGridView.setVerticalSpacing(MobileUtils.dpToPx(6));
		mGridView.setColumnWidth(gridview_item_width);
		mGridView.setCacheColorHint(getResources().getColor(R.color.touming));
		mGridView.setNumColumns(2);
		
		
		
	}

	@Override
	protected void showdata() {
		removeFootRefreshView();
		if(doctorTalkChildListBean.getNext_cursor() == doctorTalkChildListBean.getTotal_count())
		{
			mPullToRefreshView.setEnablePullLoadMoreDataStatus(false);
		}
		
		if(null != this.doctorTalkChildListBean.getObjs() && this.doctorTalkChildListBean.getObjs().size() > 0){
			if(adapter == null){
				adapter = new DoctorTalkChildListAdapter(this.mContext, bitmapUtil, gridview_item_image_height);
			}
			adapter.setList(this.doctorTalkChildListBean.getObjs());
			mGridView.setAdapter(adapter);
			this.cursor = doctorTalkChildListBean.getNext_cursor();
			this.total_count = doctorTalkChildListBean.getTotal_count();
			adapter.notifyDataSetChanged();
		}else{
			if(isFootRefresh){
				Toast.makeText(this, "没有更多数据！", 0).show();
			}else{
				Toast.makeText(this, "没有数据哦！", 0).show();
			}
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub
		final Object  obj = this.doctorTalkChildListBean.getObjs().get(position);
		Intent  intent = null;
		if(obj instanceof ArticleBean){//文章 , webview 加载文章
			intent = new Intent(this, ArticleWapActivity.class);
			intent.putExtra("article_id", ((ArticleBean)obj).getArticle_id()+"");
			
		}else if(obj instanceof CatalogBean){ // 问题 （分为一个问题 和 多个问题）
			final CatalogBean catalog = (CatalogBean)obj;
//			if(catalog.getCatalog_type() == 1){ //专题
//				if(catalog.getCatalogItems().size() > 1){ //多个专题
//					intent = new Intent(this, TopicsSheetListActivity.class);
//					intent.putExtra("catalog_id", catalog.getCatalog_id() + "");
//					
//				}else{//一个专题 (进入专题详情)
//					final CatalogTopicItem  topic = (CatalogTopicItem) catalog.getItems().get(0);
//					if(topic == null || topic.getItem() == null)return;
//					
//					intent = new Intent(this, TopicAndResultActivity.class);
//					intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID, topic.getItem().getTopic_id());
//					intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, topic.getItem().getSource_id());
//				}
//			}else if(catalog.getCatalog_type() == 3){//问题
//				if(catalog.getCatalogItems().size() > 1){ //多个问题()
//					intent = new Intent(this, CatalogsDoctorListActivity.class);
//					intent.putExtra(KidAction.DOCTOR_CATALOG_ID, catalog.getCatalog_id() + "");
//				}else{//一个问题 (进入问题详情)
//					final CatalogTopicItem  topic = (CatalogTopicItem) catalog.getItems().get(0);
//					if(topic == null || topic.getItem() == null)return;
//					
//					intent = new Intent(this,ProblemDetailsAcivity.class);
//					intent.putExtra(KidAction.DOCTOR_TOPICI_DD, topic.getItem().getTopic_id()+"");
//				}
//			}else 
				//if(catalog.getCatalog_type() == 2){ //问医生
					if(catalog.getCatalogItems().size() > 1){ 
						intent = new Intent(this, CatalogsDoctorListActivity.class);
						intent.putExtra(KidAction.DOCTOR_CATALOG_ID, catalog.getCatalog_id() + "");
					}else{
						if(catalog.getCatalogItems() ==null || "[]".equals(catalog.getCatalogItems())|| catalog.getCatalogItems().size() == 0)return;
//						if(!(catalog.getCatalogItems().get(0) instanceof CatalogTopicItem))return;
						final CatalogTopicItem  topic = (CatalogTopicItem) catalog.getCatalogItems().get(0);
						if(topic == null || topic.getItem() == null)return;
						intent = new Intent(this,ProblemDetailsAcivity.class);
						intent.putExtra(KidAction.DOCTOR_TOPICI_DD, topic.getItem().getTopic_id()+"");
					}
			//}
			
		}
		
		this.startActivity(intent);
	}

}
